<template>
  <dt-modal-edit
    ref="edit"
    :title="title"
    :loading="loading"
    :label-width="100"
    okText="确认"
    :width="800"
    @ok="onOk">
    <div slot="bottom">
      <p class="title">审批流</p>
      <dt-grid ref="table" :columns="columns" :list="list" height-resize />
    </div>
  </dt-modal-edit>
</template>

<script>
  import editOpt from './opt/auditOpt.js'
  import columnOpt from './opt/processColumn.js'
  import auditMgr from '@/biz/flowApproval/processAudit.js'
  import billMgr from '@/biz/bonus/bill'

  export default {
    data() {
      return {
        info: {},
        columns: [],
        list: [],
        title: '审批',
        loading: false,
        isShowImg: false
      }
    },
    props: {
      isGogoPrve: {
        type: Boolean,
        default: true
      }
    },
    mounted() {},
    methods: {
      show(info) {
        this.info = {
          ...info,
          auditResultFlag: '',
          auditReason: ''
        }
        this.$refs.edit.show(editOpt.options(this), this.info)
        this.columns = columnOpt.options(this)
        this.$refs.table.init(this)
        this.process()
      },
      hide() {
        this.$refs.edit.hide()
      },
      process() {
        auditMgr
          .process({
            businessId: this.info.businessId,
            auditType: this.info.auditType
          })
          .then((res) => {
            this.list = res
          })
      },
      onOk(data) {
        let auditFile = {
          annex: [],
          file: []
        }
        data.fileList &&
          data.fileList.forEach((item) => {
            auditFile.file.push({
              name: item.accessoryName,
              url: item.accessoryUrl
            })
          })
        data.picList &&
          data.picList.forEach((item) => {
            auditFile.annex.push(item.url)
          })

        if (this.info.auditType == 3 || this.info.auditType == 4 || this.info.auditType == 5) {
          billMgr.isCanApproveBonus(this.$route.query.housesId).then((res) => {
            console.log(res, '前一步')
            this.onAudit(data, auditFile)
          })
        } else {
          this.onAudit(data, auditFile)
        }
      },
      onAudit(data, auditFile) {
        this.loading = true
        console.log(auditFile, JSON.stringify(auditFile), 'auditFileauditFileauditFileauditFile')
        auditMgr
          .audit({
            auditId: data.auditId,
            auditResultFlag: data.auditResultFlag,
            auditReason: data.auditReason,
            orderAdjust: JSON.stringify(auditFile)
          })
          .then((res) => {
            this.$Message.success('操作成功')
            this.loading = false
            this.$emit('auditSuccess')
            this.hide()
            if (this.isGogoPrve) {
              this.$store.commit('deleteroutefun', this.$route.meta.title)
              this.$router.go(-1)
            }
          })
          .catch(() => {
            this.loading = false
          })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .title {
    line-height: 20px;
    border-left: 3px solid #2b85e4;
    padding-left: 8px;
    margin-bottom: 10px;
  }
</style>
